﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using EasyToHelp.DBFactory;
using EasyToHelp.WEBSystem.Helper;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace EasyToHelp.WEBSystem
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //添加认证Cookie信息
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = new PathString("/Admin/Login");
                options.AccessDeniedPath = new PathString("/denied");
            });
            services.AddMvc();
            var DbConnection = Configuration.GetSection("MySqlConnection").Get<string>();
            DBSqlHelper.connStr = DbConnection;
            //配置mysql连接字符串
            services.AddScoped<Chloe.IDbContext>((serviceProvider) =>
            {
                return new Chloe.MySql.MySqlContext(new MySqlConnectionFactory(DbConnection));
            });
            ContextHelper._connString = DbConnection;
            //容器配置
            ContainerBuilder builder = new ContainerBuilder();
            IContainer container = builder.Register_Automatic(services);
            return container.Resolve<IServiceProvider>();
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }           
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
